package com.killer.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.killer.canal.annotation.CanalEventListener;
import com.killer.canal.annotation.ListenPoint;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@CanalEventListener
@Slf4j
public class GoodsListener {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @ListenPoint(schema = "shop_goods",table = {"tb_spu"})
    public void spuUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        log.info("spu数据发生变化");
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            //一条数据变更会返回遍历所有列，只要发送一次即可
            if (column.getName().equals("id")) {
                log.info("推送");
                rocketMQTemplate.convertAndSend("spu_update", column.getValue());
            }
        }
    }
}
